package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.SetDistributionAgencyConfigReq;
import com.shop2cn.iapi.sqdistributor.parameter.request.pojo.DistributionAgencyLevelCommissionConfigVo;
import com.shop2cn.iapi.sqdistributor.service.SetDistributionAgencyConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributionAgencyCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencyConfig;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyConfigWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_SetDistributionAgencyConfig {
	
	//java接口
	private static SetDistributionAgencyConfigReq setDistributionAgencyConfigReq;
	private static SetDistributionAgencyConfigCall setDistributionAgencyConfigCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置商户的分销员机构配置");
	}

	@Before
	public void caseUp() {
		setDistributionAgencyConfigReq = new SetDistributionAgencyConfigReq();
		setDistributionAgencyConfigCall = new SetDistributionAgencyConfigCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setDistributionAgencyConfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributionAgencyConfigWrapper distributionAgencyConfigWrapper = new DistributionAgencyConfigWrapper();
		
		DistributionAgencyConfig actualResult = distributionAgencyConfigWrapper
				.selectByMerchantId(setDistributionAgencyConfigReq.getMerchantId());
		
		Logger.comment("验证distribution_agency_config表");

		Logger.verifyEquals(setDistributionAgencyConfigReq.getMerchantId(),
				actualResult.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(
				setDistributionAgencyConfigReq.getDistributionSettlementDelayDays() == null ? 0
						: setDistributionAgencyConfigReq
								.getDistributionSettlementDelayDays(),
				actualResult.getDistribution_settlement_delay_days(),
				"验证Distribution_settlement_delay_days");
		Logger.verifyEquals(true, actualResult.getSelf_purchase_commission(),
				"验证Self_purchase_commission");
		
		Logger.verifyEquals(setDistributionAgencyConfigReq.getSettlementPeriodicityType(),
				actualResult.getSettlement_periodicity_type(), "验证SettlementPeriodicityType");
		Logger.verifyEquals(setDistributionAgencyConfigReq.getSettlementWay(),
				actualResult.getSettlement_way(), "验证SettlementWay");
		Logger.verifyEquals(setDistributionAgencyConfigReq.getAgencyType(),
				actualResult.getAgency_type(), "验证AgencyType");
		
		verifyCommisionRateTable(actualResult.getId());
	}
	
	public void verifyCommisionRateTable(Long configId) {
		
		Logger.comment("验证distribution_agency_commision_rate表");
		
		List<DistributionAgencyLevelCommissionConfigVo> expectList = setDistributionAgencyConfigReq
				.getDistributionAgencyLevelCommissionConfigVos();
		
		DistributionAgencyCommisionRateWrapper commisionRateWrapper = new DistributionAgencyCommisionRateWrapper();
		
		DistributionAgencyCommisionRate actualResult = commisionRateWrapper.selectByConfigId(configId);
		DistributionAgencyLevelCommissionConfigVo expectResult = expectList.get(0);
		
		Logger.verifyEquals(setDistributionAgencyConfigReq.getMerchantId(),
				actualResult.getMerchant_id(), "验证Merchant_id");
		Logger.verifyEquals(configId,
				actualResult.getDistribution_config_id(),
				"验证Distribution_config_id");
		Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
				actualResult.getDistributor_level_index(),
				"验证Distributor_level_index");
		Logger.verifyEquals(expectResult.getDistributorLevelLabel(),
				actualResult.getDistributor_level_label(),
				"验证Distributor_level_label");
		DataUtil.verifyDecimalEquals(expectResult.getLayer1Rate(), actualResult.getLayer1_rate(), "验证Layer1_rate");
		
		if (setDistributionAgencyConfigReq.getAgencyType() == 1) {
			Logger.verifyEquals(expectResult.getUserDistributorBindRuleType(),
					actualResult.getUser_distributor_bind_rule_type(), "验证User_distributor_bind_rule_type");
		} else
			Logger.verifyEquals(0,
					actualResult.getUser_distributor_bind_rule_type(), "验证User_distributor_bind_rule_type");
			
		if (expectResult.getUserDistributorBindRuleType() == 3) {
			Logger.verifyEquals(expectResult.getBindingEffectiveThreshold(),
					actualResult.getBinding_effective_threshold(), "验证BindingEffectiveThreshold");
		} else 
			Logger.verifyEquals(0,
					actualResult.getBinding_effective_threshold(), "验证BindingEffectiveThreshold");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_001() {
		Logger.start(true, "商户的分销员机构配置，设置成功");
		
		try {

			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(1);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(500861041L);
			setDistributionAgencyConfigReq.setAgencyType(1);
			setDistributionAgencyConfigReq.setSettlementWay(0);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(0);

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(true, setDistributionAgencyConfigCall.getSuccess(), "验证success");
		
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_002() {
		Logger.start(true, "DistributionSettlementDelayDays为null，一级分销，设置成功");
		
		try {
			
			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1);
			
			distributorLevelCommissionConfigVo.setUserDistributorBindRuleType(3);
			distributorLevelCommissionConfigVo.setBindingEffectiveThreshold(100);

			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(500861041L);
			setDistributionAgencyConfigReq.setAgencyType(1);
			setDistributionAgencyConfigReq.setSettlementWay(1);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(0);

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();

			Logger.verifyEquals(true, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_003() {
		Logger.start(true, "MerchantId为null，设置失败");
		
		try {
			
			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(1);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setAgencyType(1);
			setDistributionAgencyConfigReq.setSettlementWay(1);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(0);
			
			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();

			Logger.verifyEquals(false, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("merchantId:must be greater than or equal to 1",
					setDistributionAgencyConfigCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_004() {
		Logger.start(true, "MerchantId不存在，设置成功");
		
		try {
			
			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1);
			
			distributorLevelCommissionConfigVo.setUserDistributorBindRuleType(2);
			distributorLevelCommissionConfigVo.setBindingEffectiveThreshold(31);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(1);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(123416443643623L);
			setDistributionAgencyConfigReq.setAgencyType(1);
			setDistributionAgencyConfigReq.setSettlementWay(1);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(0);
			

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();

			Logger.verifyEquals(true, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_005() {
		Logger.start(true, "商户的CPS配置，设置成功");
		
		try {

			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(2);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(28);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(500861041L);
			setDistributionAgencyConfigReq.setAgencyType(2);
			setDistributionAgencyConfigReq.setSettlementWay(0);

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(true, setDistributionAgencyConfigCall.getSuccess(), "验证success");
		
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_006() {
		Logger.start(true, "如果是周期结算，DistributionSettlementDelayDays为32，设置出错");
		
		try {

			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(2)
					.withUserDistributorBindRuleType(1)
					.withUserDistributorBindRuleType(1);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(32);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(500861041L);
			setDistributionAgencyConfigReq.setAgencyType(2);
			setDistributionAgencyConfigReq.setSettlementWay(1);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(1);

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(false, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("自定义天数发货后结算格式不正确",
					setDistributionAgencyConfigCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_007() {
		Logger.start(true, "如果是周期结算，DistributionSettlementDelayDays为0，设置出错");
		
		try {

			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(2)
					.withUserDistributorBindRuleType(1)
					.withUserDistributorBindRuleType(1);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(0);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(500861041L);
			setDistributionAgencyConfigReq.setAgencyType(2);
			setDistributionAgencyConfigReq.setSettlementWay(0);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(1);

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(false, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("自定义天数发货后结算格式不正确",
					setDistributionAgencyConfigCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetDistributionAgencyConfig_008() {
		Logger.start(true, "分销机构没有设置买家与分销关系绑定规则，设置失败");
		
		try {

			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1)
					.withUserDistributorBindRuleType(0);
			
			setDistributionAgencyConfigReq.setMerchantId(123456L);
			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(1);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setAgencyType(1);
			setDistributionAgencyConfigReq.setSettlementWay(1);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(0);
			
			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();

			Logger.verifyEquals(false, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("普通分销机构买家与分销员绑定类型不能为空",
					setDistributionAgencyConfigCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	// 取消连锁店模式
	public void Tc_SetDistributionAgencyConfig_009() {
		Logger.start(true, "商户的分销员机构-连锁店配置，设置线上支付，设置失败");
		
		try {

			DistributionAgencyLevelCommissionConfigVo distributorLevelCommissionConfigVo = DistributionAgencyLevelCommissionConfigVo
					.createStandardDistributorLevelCommissionConfig(1);

			setDistributionAgencyConfigReq.setDistributionSettlementDelayDays(1);
			setDistributionAgencyConfigReq
					.setDistributionAgencyLevelCommissionConfigVos(Arrays
							.asList(distributorLevelCommissionConfigVo));
			setDistributionAgencyConfigReq.setMerchantId(501742451L);
			setDistributionAgencyConfigReq.setAgencyType(1);
			setDistributionAgencyConfigReq.setSettlementWay(0);
			setDistributionAgencyConfigReq.setSettlementPeriodicityType(0);

			setDistributionAgencyConfigCall.setData(setDistributionAgencyConfigReq);
			setDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(false, setDistributionAgencyConfigCall.getSuccess(),
					"验证success");
			Logger.verifyEquals("商户501742451已经开启了微信直连支付通道，不能设置线上结算模式！",
					setDistributionAgencyConfigCall.getMsg(), "验证resultMessage");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
